package com.cuanbobox.uart;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Handler;
import android.os.Message;
import android.util.Log;


public abstract class Uart {

    public static final String TAG = "Uart";

    // aio-3288
    public static final String PATH_RS485 = "/dev/ttysWK3";
    public static final String PATH_RS232UP = "/dev/ttysWK1";
    public static final String PATH_RS232DOWN = "/dev/ttyS3";
    public static final String PATH_UART1 = "/dev/ttyS1";
    public static final String PATH_UART2 = "/dev/ttysWK0";
    public static final String PATH_UART3 = "/dev/ttysWK2";

    // SOM-RK3399
    public static final String PATH_UART4 = "/dev/ttyS4";
    public static final String PATH_UARTUSB0 = "/dev/ttyUSB0";
    public static final String PATH_UARTUSB1 = "/dev/ttyUSB1";
    public static final String PATH_UARTUSB2 = "/dev/ttyUSB2";
    public static final String PATH_UARTUSB3 = "/dev/ttyUSB3";
    public static final String PATH_UARTUSB4 = "/dev/ttyUSB4";

    // RK3399
    public static final String PATH_RS232_A = "/dev/ttyXRUSB2";
    public static final String PATH_RS232_B = "/dev/ttyXRUSB0";
    public static final String PATH_RS485A = "/dev/ttyXRUSB1";

    //使用串口号
    public static String PATH_TOUCH = PATH_UART4;
    public static String PATH_PASS = PATH_UARTUSB1;

    public final int BUFSIZE = 4096;
    public byte[] bufRx = new byte[BUFSIZE];

    public interface Callback {
        void onUartReceived(byte[] data, int length);
    }

    public Callback callback = null;
    public String path;
    public int baudrate;

    public Uart(Callback callback, String path, int baudrate) {
        this.callback = callback;
        this.path = path;
        this.baudrate = baudrate;
    }

    public static Uart createUart(Callback callback, String path, int baudrate) {
        try {
            return new UartSom( callback, path, baudrate );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean send(byte[] data) {
        try{
            Log.d( TAG, "data.length " + new String(data,"utf-8") );
        }catch (Exception e){

        }
        return tx( data, 0, data.length );
    }

    public boolean send(byte[] data, int offset, int length) {
        try{
            Log.d( TAG, "data.length " + new String(data,"utf-8") );
        }catch (Exception e){

        }

        return tx( data, offset, length );
    }

    public boolean sendHexMsg(byte[] data) {
		/*byte[] buf = new byte[data.length + 1];
		System.arraycopy(data, 0, buf, 0, data.length);
		buf[data.length] = (byte) 0x0A;*/
        return send( data );
    }

    public Timer timer = new Timer();

    public Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (getPortState() == false) {
                return;
            }
            switch (msg.what) {
                case 1:
                    int size = rx( bufRx, BUFSIZE );
                    if (size > 0 && callback != null) {
                        byte[] data = new byte[size];
                        System.arraycopy( bufRx, 0, data, 0, size );
                        callback.onUartReceived( data, data.length );
                    }
                    break;
            }
            super.handleMessage( msg );
        }
    };

    public TimerTask task = new TimerTask() {
        public void run() {
            Message message = new Message();
            message.what = 1;
            handler.sendMessage( message );
        }
    };

    public abstract boolean getPortState();

    public abstract boolean open();

    public abstract boolean close();

    public abstract boolean tx(byte[] data, int offset, int length);

    public abstract int rx(byte[] data, int size);

}
